package com.zzy.service.impl;

import com.zzy.dao.BaseDao;
import com.zzy.model.Catalog;
import com.zzy.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service()
public class CatalogServiceImpl implements CatalogService {


    @Autowired
    private BaseDao<Catalog> basedao;


    public List<Catalog> getRootList() {
        String hql = "from Catalog where 1=1 and (  parent is null or  parent ='' ) ";
        List list = basedao.find(hql);
        return list;
    }

    public List<Catalog> getByParentid(String parentid) {
        return null;
    }

    public int save(Catalog model) {


        // 文件名字去重
        Catalog p = model.getParent();
        String parentid = null;
        if (p != null) {
            parentid = model.getParent().getId();
        }
        //String name = model.getName();
        //getLastDirName(name, parentid);
       // model.setName(finaDir);
        //生成code

        model.setCode(getGenCode(parentid));

        String hql = " from Catalog where name =?  ";
        if(StringUtils.hasText(parentid)){
            hql += " and   parent='"+parentid+"'";
        }else {
            hql += " and ( parent='' or parent is null )";
        }

        List<Catalog> cf = basedao.find(hql,new Object[]{model.getName()});
        if(cf==null || cf.size()==0){
            basedao.save(model);
            return 1;
        }else{
            return 0;
        }


    }

    public int update(Catalog model) {
        String id = model.getId();
        Catalog old = get(id);
        // 文件名字去重
        Catalog p = old.getParent();
        String parentid = null;
        if (p != null) {
            parentid = old.getParent().getId();
        }
        if(!StringUtils.hasText(parentid)){//防止 为 空字符串
            parentid = null;
            old.setParent(null);
        }
        String name = model.getName();
        old.setName(name);
        //getLastDirName(name, parentid); ,有缓存 更新不可以使用，新增可以
        //old.setName(finaDir);//要排除自己
        List<Catalog> cf = basedao.find(" from Catalog where name =? and id != ? ",new Object[]{name,id});
        if(cf==null || cf.size()==0){
            basedao.update(old);
            return 1;
        }else{
            return 0;
        }
    }

    public Catalog get(String id) {
        return basedao.get(Catalog.class, id);
    }

    public void delete(String id) {
        if (StringUtils.hasText(id)) {
            basedao.delete(get(id));
        }
    }

    /**
     * 当前目录下文件名字是否重复
     */
    public boolean isExistName(String name, String parentid) {
        String sql = " select * from t_sys_catalog where name='" + name + "' ;";
        List<Object[]> list = new ArrayList<Object[]>();
        if (StringUtils.hasText(parentid)) {
            sql += "  and parent=" + parentid + " ";
        }
        list = basedao.getBySqlObject(sql);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }



    private String finaDir;

    public String getLastDirName(String lastName, String parentid) {
        if (isExistName(lastName, parentid)) {//有重名字的
            finaDir = lastName + "_1";
            getLastDirName(finaDir, parentid);//递归继续查询
        }
        return finaDir;
    }

    /**
     * 自动生成目录编码
     * * 编码规则
     * 1
     * 1-1
     * 1-1-1
     * 2
     * 2-1
     */
    public String getGenCode(String parentid) {
        String sql = " select * from t_sys_catalog where 1=1 ";
        boolean isp = StringUtils.hasText(parentid);
        if (isp) {
            sql = sql + " and parent='" + parentid + "' ";
        }
        sql = sql + " ORDER BY code desc LIMIT 0,1  "; //
        List<Catalog> list = new ArrayList<Catalog>();
        list = basedao.getBySql(sql, Catalog.class);
        Catalog c = new Catalog();
        String maxCode = "";// 获取当前目录的 最大 code
        if (list != null && list.size() > 0) {
            c = list.get(0);
            maxCode = c.getCode();
        }
        boolean haveMaxCode = StringUtils.hasText(maxCode);
        String lastCode = "";
        if (isp) {//不是第一级 目录
            String parentCode = maxCode.substring(0, maxCode.lastIndexOf("-"));
            ;//此时一定 有父code
            if (haveMaxCode) {//当前目录 有最大code
                //获取 该code 的最后一个数据
                String lastNum = maxCode.substring(maxCode.lastIndexOf("-") + 1, maxCode.length());
                lastCode = parentCode + "-" + (Integer.valueOf(lastNum) + 1);
            } else {//本目录下，第一个目录
                lastCode = parentCode + "-1";
            }
        } else {//一级目录
            if (haveMaxCode) {//当前目录 有最大code
                lastCode = (Integer.valueOf(maxCode.replaceAll("-", "")) + 1) + "";
            } else {//第一个目录
                lastCode = "1";
            }
        }
        return lastCode;
    }

}
